<template>
  <div class="orderDetails">
    <van-nav-bar
      title="订单详情"
      left-arrow
      @click-left="$router.push('/order')"
      @click-right="$router.push('/search')"
    >
      <template #right>
        <i class="iconfont icon-sousuo"></i>
      </template>
    </van-nav-bar>
    <div class="order_block">
      <div class="order_text">
        <p>订单编号：5220104420600891</p>
        <p v-if="item.type !== '退修换'">订单状态：等{{ item.type }}</p>
        <p v-if="item.type == '退修换'">订单状态：完成交易</p>
      </div>
    </div>
    <div class="order_info">
      <ol>
        <!-- done -->
        <li :class="{ done: item.type == '待付款' }">
          <span class="status">待付款</span
          ><span class="time">2022/01/04 10:38</span>
        </li>
        <li :class="{ done: item.type == '待发货' }">
          <span class="status">待发货</span>
        </li>
        <li :class="{ done: item.type == '待收货' }">
          <span class="status">待收货</span>
        </li>
        <li :class="{ done: item.type == '退修换' }">
          <span class="status">交易成功</span>
        </li>
      </ol>
    </div>
    <div class="order_product">
      <ul>
        <li
          class="produc_warp"
          v-for="(value, index) of item.orderList"
          :key="index"
        >
          <div class="product_block">
            <img :src="value.image_url" />
            <div class="info">
              <div class="">
                <div class="product_name">
                  <p>{{ value.name }}</p>
                </div>
                <div class="product_price">
                  <p class="price_wrap">
                    <span class="price_unit">¥&nbsp;</span
                    ><span class="prixce_num">{{ value.market_price }}</span>
                  </p>
                  <p class="product_num">x {{ value.num }}</p>
                </div>
              </div>
            </div>
          </div>
        </li>
      </ul>
    </div>
    <div class="order_orderInfo">
      <p>商品价格：{{ item.total }} 元</p>
      <p>配送费用：0 元</p>
      <p>优惠：0元</p>
      <p>应付金额：{{ item.total }}元</p>
    </div>
    <div class="order_orderInfo">
      <p>下单日期：{{ item.time }}</p>
      <p class="address">
        收货地址：<span>{{ item.address.address }}</span>
      </p>
      <p>收货人名：{{ item.address.name }} {{ item.address.tel }}</p>
      <!---->
      <p>收货时间：不限送货时间</p>
      <p>发票类型：电子普通发票</p>
      <p>发票抬头：个人</p>
    </div>
    <div class="order_action">
      <span class="btn_left" @click="del" v-if="item.type !== '退修换'"
        >取消订单</span
      >
      <span class="btn_left" v-if="item.type === '退修换'">联系客服</span>
      <span class="btn_right" v-if="item.type == '待付款'" @click="modify">
        立即付款</span
      >
      <span class="btn_right" v-if="item.type == '待发货'" @click="modify">
        立即发货</span
      >
      <span class="btn_right" v-if="item.type == '待收货'" @click="modify">
        确认收货</span
      >
      <span class="btn_right" v-if="item.type == '退修换'">申请退修换</span>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import { NavBar, Dialog } from "vant";
import { mapMutations, mapState } from "vuex";
Vue.use(NavBar);
export default {
  name: "orderDetails",
  data() {
    return {
      item: null,
      type: null,
    };
  },
  created() {
    this.item = this.$route.params.item;
  },
  computed: {
    ...mapState({ orderLists: "orderLists" }),
  },
  methods: {
    ...mapMutations({ delOrder: "delOrder", modifyOrder: "modifyOrder" }),
    del() {
      let index = this.orderLists.findIndex(
        (ele) =>
          ele.num == this.item.num &&
          ele.time == this.item.time &&
          ele.total == this.item.total
      );
      // let title =
      //   this.item.type === ""
      //     ? "确认取消订单"
      //     : this.item.type === "待付款"
      //     ? "确认支付"
      //     : this.item.type === "待发货"
      //     ? "确认发货"
      //     : this.item.type === "待收货"
      //     ? "确认收货"
      //     : "";
      Dialog.confirm({
        title: "取消订单",
      })
        .then(() => {
          this.delOrder(index);
          this.$router.push("/order");
        })
        .catch(() => {
          // on cancel
        });
    },
    modify() {
      let index = this.orderLists.findIndex(
        (ele) =>
          ele.num == this.item.num &&
          ele.time == this.item.time &&
          ele.total == this.item.total
      );
      let title =
        this.item.type === ""
          ? "确认取消订单"
          : this.item.type === "待付款"
          ? "确认支付"
          : this.item.type === "待发货"
          ? "确认发货"
          : this.item.type === "待收货"
          ? "确认收货"
          : "";
      let modifyType =
        this.item.type === "待付款"
          ? "待发货"
          : this.item.type === "待发货"
          ? "待收货"
          : this.item.type === "待收货"
          ? "退修换"
          : "";
      Dialog.confirm({
        title,
      })
        .then(() => {
          this.modifyOrder({ index, type: modifyType });
        })
        .catch(() => {
          // on cancel
        });
    },
  },
};
</script>

<style lang="less">
.orderDetails {
  background: #f5f5f5;
  .van-nav-bar {
    background-color: rgb(242, 242, 242);
    position: sticky;
    top: 0;
    .van-nav-bar__left {
      .van-icon,
      .van-nav-bar__text {
        font-size: 0.4rem;
        line-height: 46px;
        color: #8a8a8a;
      }
    }
  }
  .order_block {
    padding: 0.26rem 0.36rem;
    border-bottom: 0.2rem solid #f5f5f5;
    background: #fff;
    color: #3c3c3c;
    .order_text {
      line-height: 1.5em;
      font-size: 0.25rem;
    }
  }
  .order_info {
    border-bottom: 1px solid #f6f6f6;
    margin-bottom: 0;
    padding: 0.26rem 0.36rem;
    & > ol {
      display: flex;
      width: 100%;
      li {
        flex: 1;
        margin-right: 2px;
        list-style: none;
        .status {
          background: #ccc;
          color: #fff;
          text-align: center;
          padding: 0.1rem 0;
          margin-bottom: 0.28rem;
          display: block;
        }
        .time {
          font-size: 0.2rem;
          text-align: center;
          display: block;
        }
      }
      .done {
        .status {
          background: #0c6;
        }
      }
    }
  }
  .order_product {
    background: #fff;
    border-bottom: 0.2rem solid #f5f5f5;
    ul {
      width: 100%;
      .produc_warp {
        border-bottom: 1px solid #e7e7e7;
        list-style: none;
        .product_block {
          z-index: 2;
          padding: 0.26rem 0.36rem;
          position: relative;
          justify-content: space-between;
          align-items: center;
          display: flex;
          img {
            margin-right: 0.2rem;
            width: 1.13rem;
            height: 1.13rem;
          }
          .info {
            flex: 1 1 auto;
            & > div {
              display: flex;
              align-items: flex-start;
              justify-content: space-evenly;
              .product_name {
                width: 3.78rem;
                text-overflow: ellipsis;
                overflow: hidden;
              }
              .product_price {
                width: 1.28rem;
                text-align: right;
                .price_wrap {
                  white-space: nowrap;
                  .price_unit {
                    padding-top: 0.02rem;
                    font-size: 0.187rem;
                    display: inline-block;
                    vertical-align: 3px;
                  }
                  .prixce_num {
                    font-size: 0.28rem;
                    text-align: left;
                  }
                }
                .product_num {
                  padding-top: 0.2rem;
                  color: rgba(0, 0, 0, 0.26);
                }
              }
            }
          }
        }
      }
    }
  }
  .order_orderInfo {
    padding: 0.26rem 0.36rem;
    border-bottom: 0.2rem solid #f5f5f5;
    background: #fff;
    p {
      line-height: 1.8em;
    }
  }
  .order_action {
    background: #fff;
    position: fixed;
    bottom: 0;
    right: 0;
    left: 0;
    margin-bottom: 0;
    z-index: 99;
    box-shadow: 0 3px 14px 2px rgb(0 0 0 / 12%);
    padding: 0 0.1rem 0.2rem;
    .btn_left {
      margin: 0.2rem 0.1rem 0;
      border: 1px solid #ccc;
      padding: 0 0.3rem;
      height: 0.6rem;
      line-height: 0.6rem;
      color: #333;
      float: left;
    }
    .btn_right {
      background: #f95b07;
      border: none;
      color: #fff;
      float: right;
      margin: 0.2rem 0.1rem 0;
      padding: 0 0.3rem;
      height: 0.6rem;
      line-height: 0.6rem;
    }
  }
}
</style>